﻿using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using DeepEarth.Toolkit.Common;

namespace DeepEarth.Toolkit.Controls
{
	[TemplatePart(Name = PART_showStreetsSelector, Type = typeof(ToggleButton))]
	public class ShowStreetsSelector : MapInteractionControl
	{
		private const string PART_showStreetsSelector = "PART_showStreetsSelector";

		private ToggleButton _showStreetsSelector;

		public ShowStreetsSelector()
		{
			DefaultStyleKey = typeof(ShowStreetsSelector);
		}

		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();

			_showStreetsSelector = (ToggleButton) GetTemplateChild(PART_showStreetsSelector);
		}

		protected override void ViewChanged(Map.Core.MapView newView, Map.Core.MapView oldView)
		{
			if (_showStreetsSelector == null) return;

			_showStreetsSelector.ClearValue(ToggleButton.IsCheckedProperty);
			_showStreetsSelector.ClearValue(UIElement.VisibilityProperty);

			if (newView != null)
			{
				SetBinding(IsVisibleProperty,
				           new Binding("IsShowStreetsEnabled") {Source = newView});

				_showStreetsSelector.SetBinding(ToggleButton.IsCheckedProperty,
				                                new Binding("Mode.ShowStreets") {Source = newView, Mode = BindingMode.TwoWay});
			} 
			else
			{
				IsVisible = false;
			}
		}
	}
}
